<?php

namespace app\admin\controller;

use cmf\controller\AdminBaseController;
use think\Db;
use think\db\Query;


class TeacherController extends AdminBaseController
{
    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function index()
    {
       $name = $this->request->param('name');
       $teacher = Db::name('teacher')
           ->where(function (Query $query)use($name){
               if ($name){
                   $query->where('cmf_teacher.name','like',"%$name%");
               }
           })
           ->order('id desc')
           ->paginate('3');
       $page = $teacher->render();
       return view('index',['teacher'=>$teacher,'page'=>$page]);
    }

    public function add(){
        if (request()->isPost()){
            $add = Db::name('teacher')->insert(input());
            if($add){
            $this->success('添加成功','teacher/index');
            $this->error('添加失败');
        }
        }
        return view('add');
    }
    public function edit($id)
    {
        if (request()->isPost()){
            $data = input();
            $edit = Db::name('teacher')->update($data);
            if ($edit){
                $this->success('修改成功','teacher/index');
                $this->error('修改失败');
            }
        }
        $teacher = Db::name('teacher')->get($id);
        return view('edit',['teacher'=>$teacher]);
    }


    public function delete($id)
    {
        $del = Db::name('teacher')->delete($id);
        if ($del){
            $this->success('删除成功');
            $this->error('删除失败');
        }
    }
}
